package cn.com.huzo.webapp.controller.inventory;

import cn.com.huzo.model.entities.inventory.ReportInventoryDetailEntity;
import cn.com.huzo.model.entities.inventory.ReportDeliveredSupplierListEntity;
import cn.com.huzo.service.inventory.MaterialInventoryBookManager;
import cn.com.huzo.service.inventory.ReportInventoryDetailManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import cn.com.huzo.service.inventory.ReportDeliveredSupplierListManager;

/**
 * 库存相关统计、分析、报告
 *
 * @author 吴旭
 */
@Controller
@RequestMapping(value = "/inventory/report")
public class InventoryReportController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "MaterialInventoryBookManager")
    private MaterialInventoryBookManager _manager_material_inventory_book;

    @Resource(name = "ReportDeliveredSupplierListManager")
    private ReportDeliveredSupplierListManager _manager_supplier_query;

    @Resource(name = "ReportInventoryDetailManager")
    private ReportInventoryDetailManager _manager_inv_detail;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 材料库存台账
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/material_inventory_book/list.html")
    public ModelAndView redirectMaterialInventoryBookWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("inventory.material_inventory_book.list.def");
        return view;
    }

    /**
     * 到货供应商列表查询画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/supplier_query/list.html")
    public ModelAndView redirectSupplierQueryWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("inventory.report_supplier_query.list.def");
        return view;
    }

    /**
     * 入库明细查询画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/inv_detail/list.html")
    public ModelAndView redirectInvDetailWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("inventory.report_inv_detail.list.def");
        return view;
    }
    
    /**
     * 备料单查询画面
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("/inv_stock_out_detail/list.html")
    public ModelAndView redirectInvStockOutDetailWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("inventory.report_inv_stock_out_detail.list.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 根据条件取得到材料库存台账数据。
     *
     * @param criterion 搜索条件
     * @return 得到该款材料库存台账。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/material_inventory_book/fetch.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchMaterialInventoryBook(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_material_inventory_book.fetchMaterialInventoryBook(
                MapUtils.getString(criterion, "StyleNo"),
                MapUtils.getString(criterion, "MaterialNo")
        );
    }

    /**
     * 根据条件取得到货供应商一览表数据。
     *
     * @param criterion 搜索条件
     * @return 得到货供应商。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/supplier_query/fetchAll.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<ReportDeliveredSupplierListEntity> fetchSupplierQuery(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_supplier_query.fetchReportData(criterion);
    }

    /**
     * 根据条件取得到入库明细数据。
     *
     * @param criterion 搜索条件
     * @return 入库明细。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/inv_detail/fetchAll.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<ReportInventoryDetailEntity> fetchInvDetail(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_inv_detail.fetchReportData(criterion);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="导出excel和打印预览">
    /**
     * 导出材料库存台账excel
     * <p>
     * @param request 客户端请求
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/material_inventory_book/export_to_excel.html",
            method = RequestMethod.POST)
    public ModelAndView materialInventoryBookExportViewExcel(HttpServletRequest request) throws Exception {
        String styleNo = request.getParameter("_ExportStyleNo").trim();
        String materialNo = request.getParameter("_ExportMaterialNo").trim();

        Map<String, Object> criterion = new HashMap();
        criterion.put("StyleNo", styleNo);
        Map<String, Object> model = _manager_material_inventory_book.fetchMaterialInventoryBook(styleNo, materialNo);

        return new ModelAndView("MaterialInventoryBookExcelView", model);
    }
    // </editor-fold>
}
